package com.example.music.controller;

import com.example.music.domain.Product;
import com.example.music.service.IProductService;
import com.example.music.util.AjaxResult;
import com.example.music.util.Anonymous;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Anonymous
@Api(tags = "音乐周边产品信息管理(224 魏川子)")
@RestController
@RequestMapping("/api/product")
public class ProductController {
    @Autowired
    private IProductService productService;

    @ApiOperation(value = "获取所有产品信息(224 魏川子)", notes = "获取所有产品信息")
    @GetMapping("/list")
    public AjaxResult list() {
        return AjaxResult.success(productService.getProducts());
    }

    @ApiOperation(value = "根据名字获取产品信息(224 魏川子)", notes = "根据名字获取产品信息")
    @GetMapping("/getProductByName/{pName}")
    public AjaxResult getProductByName(@PathVariable("pName") String pName) {
        return AjaxResult.success(productService.selectProductByName(pName));
    }

    @ApiOperation(value = "根据id获取产品信息(224 魏川子)", notes = "根据id获取产品信息")
    @GetMapping("/{id}")
    public AjaxResult getProductById(@PathVariable("id") Long id) {
        return AjaxResult.success(productService.selectProductById(id));
    }

    @ApiOperation(value = "新增产品信息(224 魏川子)", notes = "新增产品信息")
    @PostMapping("/insert")
    public AjaxResult insertProduct(@RequestBody Product product) {
        return AjaxResult.success(productService.insertProduct(product));
    }

    @ApiOperation(value = "更新产品信息(224 魏川子)", notes = "更新产品信息")
    @PutMapping("/update")
    public AjaxResult updateProduct(@Validated @RequestBody Product product) {
        return AjaxResult.success(productService.updateProduct(product));
    }

    @ApiOperation(value = "删除产品信息(224 魏川子)", notes = "删除产品信息")
    @DeleteMapping("/{id}")
    public AjaxResult deleteProduct(@PathVariable Long id) {
        return AjaxResult.success(productService.deleteProductById(id));
    }
}
